package com.ruix.app.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.model.Comment;
import com.ruix.app.model.PostsCard;

/**
 * Created by hadexs on 2017/8/6.
 */

public class CommentDialog extends Dialog{


    private PostsCard item;
    private Comment replayComment;
    private EditText leaveWords;
    private TextView sendMessage;
    private CommentDialog.CommentListener listener;


    public static CommentDialog newInstance(Context context,PostsCard postsCard,Comment comment,CommentDialog.CommentListener commentListener){
        CommentDialog dialog = new CommentDialog(context,R.style.Dialog);
        dialog.replayComment = comment;
        dialog.item = postsCard;
        dialog.listener = commentListener;
        return dialog;
    }

    public CommentDialog(@NonNull Context context) {
        super(context);
    }


    public CommentDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_circle_comment);
        setCanceledOnTouchOutside(true);

        Window dialogWindow = getWindow();

        dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        dialogWindow.setGravity(Gravity.BOTTOM);

        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;//宽高可设置具体大小
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        initView();
        initListener();
    }

    private void initView(){
        leaveWords = (EditText) findViewById(R.id.et_leave_words);
        sendMessage = (TextView) findViewById(R.id.tv_send_message);
        leaveWords.requestFocus();
    }

    public void initHint(){
        if (replayComment != null) {
            String name = replayComment.getAuthorName();
            if (!TextUtils.isEmpty(name)) {
                leaveWords.setHint(String.format("回复：%s", name));
            }
        } else if (item != null){
            if (!TextUtils.isEmpty(item.getUserName())) {
                leaveWords.setHint(String.format("评论：%s", item.getUserName()));
            }
        }
    }

    private void initListener(){
        leaveWords.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0){
                    sendMessage.setEnabled(true);
                } else {
                    sendMessage.setEnabled(false);
                }
            }
        });
        sendMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null){
                    listener.submitCommentAction(leaveWords.getText().toString().trim());
                }
                dismiss();
            }
        });
    }

    public interface CommentListener{
        void  submitCommentAction(String commentStr);
    }
}
